package cn.hrlly.tao2shou.view.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.ImageLoader;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.hrlly.tao2shou.R;
import cn.hrlly.tao2shou.domain.User;
import cn.hrlly.tao2shou.presenter.QueryUserInfoPresenter;
import cn.hrlly.tao2shou.presenter.QueryUserInfoPresenterImpl;
import cn.hrlly.tao2shou.utils.ImageUtil;
import cn.hrlly.tao2shou.view.activity.BuyerApplicationActivity;
import cn.hrlly.tao2shou.view.activity.BuyerOrderActivity;
import cn.hrlly.tao2shou.view.activity.HistoryActivity;
import cn.hrlly.tao2shou.view.activity.MyCommodityActivity;
import cn.hrlly.tao2shou.view.activity.MyRequireActivity;
import cn.hrlly.tao2shou.view.activity.SellerApplicationActivity;
import cn.hrlly.tao2shou.view.activity.SellerOrderActivity;
import cn.hrlly.tao2shou.view.activity.SettingActivity;
import cn.hrlly.tao2shou.view.view_interface.QueryUserInfoView;

public class MyFragment extends Fragment implements QueryUserInfoView {
    @Bind(R.id.ivUser)
    ImageView ivUser;
    @Bind(R.id.ivPortrait)
    ImageView ivPortrait;
    @Bind(R.id.tvNickname)
    TextView tvNickname;
    @Bind(R.id.layoutBuyserApplication)
    LinearLayout layoutBuyerApplication;
    @Bind(R.id.layoutSellerApplication)
    LinearLayout layoutSellerApplication;
    @Bind(R.id.layoutHistory)
    LinearLayout layoutHistory;
    @Bind(R.id.layoutCommodity)
    LinearLayout layoutCommodity;
    @Bind(R.id.layoutRequire)
    LinearLayout layoutRequire;
    @Bind(R.id.layoutSetting)
    LinearLayout layoutSetting;
    @Bind(R.id.layoutBuyerOrder)
    LinearLayout layoutBuyerOrder;
    @Bind(R.id.layoutSellerOrder)
    LinearLayout layoutSellerOrder;
    private QueryUserInfoPresenter queryUserInfoPresenter;
    private ImageLoader imageLoader;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_my, container, false);
        ButterKnife.bind(this, rootView);
        imageLoader = ImageLoader.getInstance();
        queryUserInfoPresenter = new QueryUserInfoPresenterImpl(this, getActivity());
        //查询用户信息
        queryUserInfoPresenter.queryUserInfo();
        return rootView;
    }

    //以下都是点击事件
    @OnClick({R.id.layoutBuyserApplication, R.id.layoutSellerApplication,R.id.layoutBuyerOrder ,R.id.layoutSellerOrder,R.id.layoutHistory, R.id.layoutCommodity, R.id.layoutRequire, R.id.layoutSetting})
    public void buyer(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.layoutBuyserApplication:
                intent = new Intent(getActivity(), BuyerApplicationActivity.class);
                startActivity(intent);
                break;
            case R.id.layoutSellerApplication:
                intent = new Intent(getActivity(), SellerApplicationActivity.class);
                startActivity(intent);
                break;
            case R.id.layoutBuyerOrder:
                intent = new Intent(getActivity(), BuyerOrderActivity.class);
                startActivity(intent);
                break;
            case R.id.layoutSellerOrder:
                intent = new Intent(getActivity(), SellerOrderActivity.class);
                startActivity(intent);
                break;
            case R.id.layoutHistory:
                intent = new Intent(getActivity(), HistoryActivity.class);
                startActivity(intent);
                break;
            case R.id.layoutCommodity:
                intent = new Intent(getActivity(), MyCommodityActivity.class);
                startActivity(intent);
                break;
            case R.id.layoutRequire:
                intent = new Intent(getActivity(), MyRequireActivity.class);
                startActivity(intent);
                break;
            case R.id.layoutSetting:
                intent = new Intent(getActivity(), SettingActivity.class);
                startActivity(intent);
                break;
            default:
                break;
        }
    }

    //以下是对fragment的操作
    @Override
    public void showUserInfo(User user) {
        imageLoader.displayImage(user.getHeadPortrait(), ivPortrait, ImageUtil.getOptions());
        tvNickname.setText(user.getNickname());
    }

    @Override
    public void showMessage(int errorMessage) {
        Toast.makeText(getActivity(), errorMessage, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showMessage(String errorMessage) {
        Toast.makeText(getActivity(), errorMessage, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
